
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class SettingsDialogView extends JDialog {
	/**Referencja do kontrolera okna ustawień*/
	SettingsDialog controller;
	
	public JRadioButton sound1, sound2, sound3;
	public JCheckBox remind;
	public JButton normal,low,high;
	private JButton ok;
	
	/**
	 * Tworzy panel z polami wyboru.
	 * @return Gotowy panel
	 */
	private JPanel createRadioPanel ()
	{
		ButtonGroup group = new ButtonGroup();
        group.add(sound1);
        group.add(sound2);
        group.add(sound3);
		JPanel radioPanel = new JPanel(new GridLayout(0, 1));
		radioPanel.add(remind);
        radioPanel.add(sound1);
        radioPanel.add(sound2);
        radioPanel.add(sound3);
        radioPanel.setBorder(BorderFactory.createTitledBorder("Przypomnienia"));
        return radioPanel;
	}
	/**
	 * Tworzy panel przycisków.
	 * @return Gotowy panel.
	 */
	private JPanel colorButtonPanel ()
	{
		JPanel buttons = new JPanel (new GridLayout(0,3));
		buttons.add(low);
		buttons.add(normal);
		buttons.add(high);
		buttons.setBorder(BorderFactory.createTitledBorder("Kolorowanie priorytetów"));
		return buttons;
	}
	
	/**
	 * Tworzy obiekt klasy SettingsDialog view, ktora służy jako widok dla okna ustawień. Inicjalizuje i dodaje komponenty.
	 * Na początku okno jest ukryte.
	 * @param owner Rodzic okna.
	 * @param title Tytuł okna.
	 * @param controller Kontroler okna.
	 */
	SettingsDialogView (Frame owner, String title, SettingsDialog controller)
	{
		super(owner,title,true);
		this.controller=controller;
		remind = new JCheckBox("Przypominaj");
		sound1 = new JRadioButton("Gong");
		sound2 = new JRadioButton("Dzwon");
		sound3 = new JRadioButton("Dzwonek");
		
		low = new JButton("niski");
		normal = new JButton ("normalny");
		high = new JButton ("wysoki");
		setLayout(new BorderLayout());
		JPanel fields = new JPanel(new GridLayout(0,1));
		fields.add(createRadioPanel());
		fields.add(colorButtonPanel());
		JPanel buttons = new JPanel();
        buttons.setLayout(new FlowLayout());
        add(fields,BorderLayout.NORTH);
        buttons.add(ok = new JButton("OK"));
        add(buttons,BorderLayout.SOUTH);
        ButtonsListener bL = new ButtonsListener();
        low.addActionListener(bL);
        normal.addActionListener(bL);
        high.addActionListener(bL);
        ok.addActionListener(bL);
        ok.setMnemonic(KeyEvent.VK_O);
        remind.addActionListener(bL);
        sound1.addActionListener(bL);
        sound2.addActionListener(bL);
        sound3.addActionListener(bL);
		setSize(290, 300);
	}
	/**
	 * Nasłuchuje przyciski w oknie dialogowym oraz wywołuje odpowiednie metody z controller.
	 * @author krzys
	 *
	 */
	class ButtonsListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==low){controller.colourPick(0); return;}
			if(e.getSource()==normal){controller.colourPick(1); return;}
			if(e.getSource()==high){controller.colourPick(2); return;}
			if(e.getSource()==ok){controller.ok();}
			if (e.getSource()==remind){controller.setRemind(remind.isSelected());}
			if(e.getSource()==sound1){controller.setSoundID(1); return;}
			if(e.getSource()==sound2){controller.setSoundID(2); return;}
			if(e.getSource()==sound3){controller.setSoundID(3); return;}
			
		}
	}
}
